package com.boxman;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.imageio.ImageIO;

// 箱子
class Box extends BaseGameObject {
    private BaseGameObject mBack;   // 背后对象
    protected Image mCurrentImage;  // 当前图片
    protected Image mImage2;        // 图片2

    /**
     * 构造方法。
     * @param back      背后对象
     * @param drawer    绘图者
     * @param type      类型
     * @param x, y      左上角坐标
     */
    public Box(BaseGameObject back, Graphics drawer, Type type, int x, int y) {
        super(drawer, type, x, y);

        // 初始化成员
        mBack = back;
        mCurrentImage = mImage;
        final String imageFilePath = String.format(Const.IMAGE_FILE_PATH, type.ordinal() * 10 + 1);
        try {
            final File file = new File(imageFilePath);
            mImage2 = ImageIO.read(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 移动。
     * @param direction     方向
     * @param moveResult    移动结果
     * @return 被推动的箱子
     */
    @Override
    public BaseGameObject move(int direction, MoveResult moveResult) {
        boolean leftFromDest = false;   // 是否离开目的地
        int destX = mX, destY = mY;     // 新位置的坐标

        // 计算新位置
        if (direction == KeyEvent.VK_LEFT) {
            destX--;
        } else if (direction == KeyEvent.VK_RIGHT) {
            destX++;
        } else if (direction == KeyEvent.VK_UP) {
            destY--;
        } else if (direction == KeyEvent.VK_DOWN) {
            destY++;
        }

        // 如果旁边是墙或箱子，则直接返回
        if (Game.sBaseGameObjects[destY][destX].getType() == Type.WALL || 
            Game.sBaseGameObjects[destY][destX].getType() == Type.BOX) {
            moveResult.result = MoveResult.Result.NO_MOVE;
            return null;
        }

        // 设置移动结果
        if (moveResult.result.ordinal() < MoveResult.Result.NORMAL_MOVED.ordinal()) {
            moveResult.result = MoveResult.Result.NORMAL_MOVED;
        }

        // 判断是否要离开目的地
        if (mType == Type.BOX && mBack.getType() == Type.DESTINATION) {
            leftFromDest = true;
            moveResult.result = MoveResult.Result.LEFT_FROM_DEST;
            mCurrentImage = mImage;
        }

        // 用背后对象擦除自己
        mBack.draw();
        Game.sBaseGameObjects[mY][mX] = mBack;

        // 计算新位置
        mX = destX;
        mY = destY;

        // 设置新的背后对象
        mBack = Game.sBaseGameObjects[mY][mX];

        // 判断是否到达目的地
        if (mType == Type.BOX && mBack.getType() == Type.DESTINATION) {
            if (leftFromDest) {
                moveResult.result = MoveResult.Result.NORMAL_MOVED;
            } else {
                moveResult.result = MoveResult.Result.ARRIVED_ON_DEST;
            }
            mCurrentImage = mImage2;
        }

        // 重绘自己
        mDrawer.drawImage(mCurrentImage, mX * Const.GRID_SIZE, mY * Const.GRID_SIZE, null);

        // 将自己保存到数组中
        Game.sBaseGameObjects[mY][mX] = this;

        return this;
    }
}
